package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.BookingRecord;
import org.example.domain.InoculationRecord;
import org.example.service.InoculationRecordService;
import org.example.utils.ResponseUtil.ResponseResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

import static org.example.utils.ResponseUtil.StatusCode.*;

@Slf4j
@RestController
@RequestMapping("/inoculation")
public class InoculationRecordController {

    @Resource
    private InoculationRecordService inoculationRecordService;


    /**
     * 增加疫苗接种记录，
     * 通过预约记录某行的"确认接种"按钮。
     */
    @RequestMapping("/insertInoculationByBooking")
    public ResponseResult insertInoculationByBooking(@RequestBody BookingRecord bookingRecord){
        System.out.println("执行了insertInoculationByBooking方法。"+ bookingRecord);
        //如果数据存在空值
        if (bookingRecord ==null|| bookingRecord.getBr_id()==null || bookingRecord.getUser_uid()==null || bookingRecord.getBooking_time()==null
                || bookingRecord.getBr_id()==0 || bookingRecord.getUser_uid() == 0 || bookingRecord.getBooking_time().equals("") ){
            return new ResponseResult(DATA_NOT_EXIST, "新增疫苗接种记录失败，信息没有填写完整！");
        }
        return  inoculationRecordService.insertInoculationByBooking(bookingRecord);
    }


    /**
     * 增加 疫苗接种记录:
     * 不可缺少的数据是用户的User_uid。
     * 预约时间如果为空，为其赋默认值，
     * 接种时间如果为空，就为其赋系统的当前时间。
     * 接种次数会通过统计数据库中的已有接种次数获得。
     */
    @RequestMapping("/insertInoculationRecord")
    public ResponseResult insertInoculationRecord(@RequestBody InoculationRecord record){
        System.out.println("执行了insertInoculationRecord方法。"+record);
        //如果添加的数据存在空值
        if (record==null|| record.getUser_uid()==null || record.getUser_uid() == 0 ){
            return new ResponseResult(DATA_NOT_EXIST, "用户id为空，接种信息添加失败！");
        }
        return  inoculationRecordService.insertInoculationRecord(record);

    }


    /**
     * 修改 记录：根据接种记录的id
     */
    @RequestMapping("/updateInoculation")
    public ResponseResult updateInoculationById(@RequestBody InoculationRecord record){
        System.out.println("执行了updateInoculationById方法。"+record);
        return inoculationRecordService.updateInoculationById(record);

    }


    /**
     * 逻辑删除：根据接种记录表的ir_id
     */
    @RequestMapping("/deleteInoculationById/{id}")
    public ResponseResult deleteInoculationById(@PathVariable("id") Integer id){
        System.out.println("执行了deleteInoculationById方法。id="+id);
        return inoculationRecordService.deleteInoculationById(id);
    }



    /**
     * 根据用户UserId查找多个接种记录
     */
    @RequestMapping("/selectInoculationByUserId/{userId}")
    public ResponseResult selectInoculationByUserId(@PathVariable("userId") Integer userId){
        System.out.println("执行了selectInoculationByUserId方法。userId="+userId);
        return  inoculationRecordService.selectInoculationByUserId(userId);
    }


    /**
     * 根据某一个查询条件，查找疫苗接种记录
     */
    @RequestMapping("/selectInoculationByArg")
    public ResponseResult selectInoculationByArg(@RequestBody Map<String,Object> map){
        System.out.println("执行了selectInoculationByArg方法！");
        if (map == null || map.size() == 0){
            return new ResponseResult(DATA_NOT_EXIST, "查询条件为空,请填写！");
        }

        //获取map中的查询条件和待匹配的值
        for (Map.Entry<String,Object> entry:map.entrySet()){
            if (entry.getKey()==null || entry.getKey().equals("") ||
                    entry.getValue()==null || entry.getValue().equals("")){
                return new ResponseResult(DATA_NOT_EXIST,"查询失败，查询条件为空！");
            }

            String key = entry.getKey();//获取key，即查询条件
            String  value1;  //用于存储 String类型的待匹配值
            Integer value2;  //用于存储 Integer类型的待匹配值


            //根据用户的userId精确匹配
            if ("userId".equals(key)){
                value2 = Integer.parseInt( (String) entry.getValue() ) ;
                System.out.println("查询条件：key="+key+"，\t值value="+value2);
                return inoculationRecordService.selectInoculationByUserId(value2);
            }
            //模糊查询userAccount
            else if ("userAccount".equals(key)){
                value1 = (String) entry.getValue();
                System.out.println("查询条件：key="+key+"，\t值value="+value1);
                return inoculationRecordService.selectInoculationLikeUserAccount(value1);
            }
            //模糊查询username
            else if ("username".equals(key)){
                value1 = (String) entry.getValue();
                System.out.println("查询条件：key="+key+"，\t值value="+value1);
                return inoculationRecordService.selectInoculationLikeUsername(value1);
            }
            //模糊查询接种时间，只根据日期2022-01-15查，忽略后面的接种时间
            else if ("inoculateDate".equals(key)){
                if (entry.getValue().toString().length()<10){
                    return new ResponseResult(DATA_NOT_EXIST,"查询失败，日期输入有误！");
                }
                value1 = entry.getValue().toString().substring(0,10);
                System.out.println("查询条件：key="+key+"，\t值value="+value1);
                return inoculationRecordService.selectInoculationLikeInoculate_time(value1);
            }
            //根据接种次数
            else if ("count".equals(key)){
                value2 = Integer.parseInt( (String) entry.getValue() ) ;
                System.out.println("查询条件：key="+key+"，\t值value="+value2);
                return inoculationRecordService.selectInoculationByCount(value2);
            }

        }

        return new ResponseResult(DATA_NOT_EXIST,"查询失败，查询条件有误！");
    }



    /**
     * 根据接种时间升序查询 所有疫苗接种记录
     */
    @RequestMapping("/AscSelectAllByTime")
    public ResponseResult AscSelectAllByTime(){
        System.out.println("执行了AscSelectAllByTime方法。");
        return inoculationRecordService.AscSelectAllByTime();
    }


    /**
     * 根据接种时间降序查询 所有疫苗接种记录
     */
    @RequestMapping("/DescSelectAllByTime")
    public ResponseResult DescSelectAllByTime(){
        System.out.println("执行了DescSelectAllByTime方法。");
        return inoculationRecordService.DescSelectAllByTime();
    }


    /**
     * 查看所有疫苗接种记录
     */
    @RequestMapping("/selectAllInoculation")
    public ResponseResult selectAllInoculation(){
        System.out.println("执行了selectAllInoculation方法。");
        return inoculationRecordService.selectAllInoculation();
    }


}
